package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.Courses;

public class CoursesDao extends SqlBase<Courses> {
	private Connection conn;

	public CoursesDao() {
		conn = DBUtile.getConnection();
	}

	public List<Courses> getCourses(Integer id) {
		List<Courses> list = new ArrayList<>();
		String sql = "select * from courses where id in (select courses_id from major_courses where major_id in(select id from major where id=?))";
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setInt(1, id);
			ResultSet rs = pstm.executeQuery();
			while (rs.next()) {
				Courses cs = new Courses();
				cs.setId(rs.getInt("id"));
				cs.setName(rs.getString("name"));
				list.add(cs);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

}
